package com.zzyl.common.ai;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.core.auth.Auth;
import com.baidubce.qianfan.model.chat.ChatResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @version 1.0
 * @description 说明
 * @package com.zzyl.common.ai
 */
@Slf4j
@Component
public class AIModelInvoker {

    @Autowired
    private BaiduAIProperties baiduAIProperties;

    public String qianfanInvoker(String prompt) {
        System.out.println(prompt);
        Qianfan qianfan = new Qianfan(Auth.TYPE_OAUTH, baiduAIProperties.getAccessKey(), baiduAIProperties.getSecretKey());
        ChatResponse response = qianfan.chatCompletion()
            .model(baiduAIProperties.getQianfanModel())
            //addMessage: 提问ai的内容, user 表示用户, prompt提问词条
            .addMessage("user", prompt)
            .temperature(0.7)
            //返回的最大数字符
            .maxOutputTokens(2000)
            //返回结果的格式json
            .responseFormat("json_object")
            .execute();
        String result = response.getResult();
       log.info("百度千帆结果：{}", result);

        return result;
    }
}
